package com.a23qws.stop;

import java.util.ArrayList;
import java.util.List;

import com.a23qws.stop.ListViewWithHeadView.ScrollListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity implements ScrollListener {

	private LinearLayout suspendView, topView, include;
	private ListViewWithHeadView listView;
	private MyAdapter adapter;
	private List<String> list;
	private RelativeLayout parent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		parent = (RelativeLayout) findViewById(R.id.parent);

		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		listView = new ListViewWithHeadView(getApplication());
		parent.addView(listView, params2);

		suspendView = (LinearLayout) LayoutInflater.from(getApplication())
				.inflate(R.layout.toplayout, null);
		topView = (LinearLayout) LayoutInflater.from(getApplication()).inflate(
				R.layout.topview, null);
		listView.addHeaderView(topView);
		listView.addHeaderView(suspendView);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    			LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		include = (LinearLayout) LayoutInflater.from(getApplication()).inflate(
				R.layout.toplayout, null);
		parent.addView(include, params);
		include.setVisibility(View.INVISIBLE);

		setContentView(parent);
		initData();
		adapter = new MyAdapter(getApplication(), list);
		listView.setAdapter(adapter);
		listView.setScrollListener(this);
	}

	private void initData() {

		list = new ArrayList<String>();
		for (int i = 0; i < 20; i++) {
			list.add("测试数据" + i);
		}
	}

	@Override
	public void scroll() {
		if (listView.getFirstVisiblePosition() >= 1) {
			include.setVisibility(View.VISIBLE);
			ListViewWithHeadView.flg = true;
		} else {
			include.setVisibility(View.INVISIBLE);
			ListViewWithHeadView.flg = false;
		}
	}
}